package com.zsh.console.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.zsh.console.dao.SearchException;
import com.zsh.console.model.Pic;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class PicAction extends BaseAction implements Preparable {
    private static final long serialVersionUID = 58938712115191L;
    private List<Pic> pics;
    private Pic pic;
    private String id;
    private String query;
    private String newPassword;
    private String confirmPassword;
    public static DateFormat sdf = new SimpleDateFormat("MM-dd");

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        // prevent failures on new
        if (getRequest().getMethod().equalsIgnoreCase("post") && (!"".equals(getRequest().getParameter("pic.id")))) {
            pic = picManager.getPic();
        }
    }

    public List<Pic> getPics() {
        return pics;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Pic getPic() {
        return pic;
    }

    public void setUser(Pic pic) {
        this.pic = pic;
    }

    public void setQ(String q) {
        this.query = q;
    }

//    public String delete() {
//        picManager.trustAndDelete(pic.getPicname());
//        List<Object> args = new ArrayList<Object>();
//        args.add(pic.getPicname());
//        saveMessage("ok,finish");
//        return SUCCESS;
//    }

    public String trustAndDelete() {
        picManager.trustAndDelete(id);
        return SUCCESS;
    }

    public String dealCheat() {
        picManager.dealCheat(id);
        return SUCCESS;
    }

    public String execute() {
        return SUCCESS;
    }

    public String cancel() {
        if (!"list".equals(from)) {
            return "home";
        }
        return "cancel";
    }

    public String list() {
        try {
            pics = picManager.search(query);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            pics = picManager.getPics();
        }
        return SUCCESS;
    }

}
